package cur.nature.tinyorm.core.template;

import cur.nature.tinyorm.core.proxy.AbstractTemplateProxy;
import cur.nature.tinyorm.core.proxy.JdbcTemplateProxyBean;
import cur.nature.tinyorm.core.proxy.TemplateProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Connection;

public class DefaultJdbcTemplate extends JdbcTemplate {
    private static Logger LOG = LoggerFactory.getLogger(DefaultJdbcTemplate.class);

    private String databaseType;
    private TemplateProxy templateProxy;

    public static final String TEMPLATE = "defaultJdbcTemplate";
    public static final String DATASOURCE = "defaultJdbcDatasource";

    //设置数据连接池
    public DefaultJdbcTemplate(DataSource dataSource){
        super(dataSource);
        // 设置连接的数据库类型
        try (Connection connection = getDataSource().getConnection()) {
            databaseType = connection.getMetaData().getDatabaseProductName();
            // 设置proxy
            this.templateProxy = new JdbcTemplateProxyBean(this, databaseType);
        } catch (Exception ex) {
            LOG.error("conn err: ",ex );
        }
    }

    public String getDatabaseType() {
        return databaseType;
    }

    public TemplateProxy getTemplateProxy() {
        return templateProxy;
    }
}
